//
// Created by Administrator on 2025/3/20.
//

#include "ui_elf_buy_prop_tip.h"

static void ui_refrsh_buy_prop_tip_screen(elf_game_module_t *module);

static void ui_elf_buy_prop_tip_enter_key(lv_group_t *group, lv_obj_t *obj, int data){
    LOG_INFO_WRAPPER("start data:%d", data);
    if(data == 0){
        // 返回
    }else if(data == 1){
        // 取消 删除当前屏幕，恢复上一个屏幕
//        ui_elf_buy_prop_tip_screen_deinit(&elf_game_module);
        ui_elf_refresh_last_screen();
        // 恢复定时器
        ui_elf_game_resume_flutter_show_and_hide_timer();




    }else if(data == 2){

        // 判断金币是否足够
        prop_data_t *prop = get_prop_info_by_id(elf_game_module.data->current_use_prop_id);
        if(elf_game_module.data->coin >= prop->coin){
            // 金币足够 跳到数据同步页面
            elf_buy_prop_data_sync_controller();
//            elf_game_use_prop_common_dao_controller(prop, 1);
        }else{
            // 确认
            ui_elf_refresh_last_screen();
            ui_elf_game_resume_flutter_show_and_hide_timer();
            // 金币不足
            ui_show_use_prop_prompt(NULL, 1);
        }
    }
}

static void ui_elf_buy_prop_tip_screen_event(lv_event_t *e){
    LOG_INFO_WRAPPER("start");
//    lv_group_send_data()
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    lv_group_t *group = lv_group_get_default();
    int event_data = (int)lv_event_get_user_data(e);
    if (event_code == LV_EVENT_KEY)
    {
        // 按键事件 获取按键值
        uint32_t lv_key = lv_event_get_key(e);
        switch (lv_key)
        {
            case LV_KEY_UP:
                LV_LOG_USER("=========== touched KeyUp");
//                ui_word_query_keyboard_left_key(group,target,event_data);
                break;
            case LV_KEY_DOWN:
                LV_LOG_USER("============ touched KeyDown");
//                ui_word_query_right_keyboard_key(group, target, event_data);
                break;
            case LV_KEY_LEFT:
                LV_LOG_USER("=========== touched KeyLeft");
//                lv_group_focus_obj(event_data->prev_obj);
//                ui_word_query_keyboard_left_key(group,target,event_data);
                lv_group_focus_prev(group);

                break;
            case LV_KEY_RIGHT:
                LV_LOG_USER("============ touched KeyRight");
//                lv_group_focus_obj(event_data->next_obj);
//                ui_word_query_right_keyboard_key(group, target, event_data);
                lv_group_focus_next(group);

                break;
            case LV_KEY_ENTER:
                LV_LOG_USER("============ touched KeyEnter");
                ui_elf_buy_prop_tip_enter_key(group,target,event_data);

                break;
            case LV_KEY_BACKSPACE:
                LV_LOG_USER("============ touched KeyBackspace");

                break;
            case LV_KEY_DEL:
                // 模拟删除字母
                LV_LOG_USER("============ touched keydel");
//                word_query_keyboard_del_key(group, target, event_data);
                break;
            case LV_KEY_SPACE:
                // 模拟输入字母
                LV_LOG_USER("============ touched space");
                break;
            case LV_KEY_ESC:
                LV_LOG_USER("============ touched ESC");
                break;
            default:
                break;
        }
    }
}

void ui_elf_buy_prop_tip_screen_init(ui_elf_game_main_screen_t *screen){
    lv_coord_t screen_width = lv_disp_get_hor_res(NULL);
    lv_coord_t screen_height = lv_disp_get_ver_res(NULL);
    if(screen->screen == NULL) {
        if (screen->group == NULL) {
            screen->group = ui_set_indev_group(indev_keypad);
        }
        screen->screen = ui_create_default_screen(lv_scr_act());

        // 屏幕panel
        lv_obj_t *main_panel = ui_create_column_default_panel(screen->screen);

        // top panel
        lv_obj_t *top_panel = ui_word_query_top_panel_component(main_panel);
        lv_obj_t *top_return_panel = lv_obj_get_child(top_panel, 1);
        lv_obj_set_style_border_side(top_return_panel, LV_BORDER_SIDE_BOTTOM,LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(top_return_panel, 2, LV_STATE_FOCUSED);
        lv_group_add_obj(screen->group, top_return_panel);
        lv_obj_add_event_cb(top_return_panel,ui_elf_buy_prop_tip_screen_event,LV_EVENT_KEY,0);
        lv_coord_t top_panel_height = lv_obj_get_style_height(top_panel, 0);
        LOG_INFO_WRAPPER("top_panel_height: %d", top_panel_height);

        // middle panel row_flex
        lv_coord_t middle_panel_height = screen_height - top_panel_height;
        lv_obj_t *middle_panel = ui_create_without_flex_panel(main_panel, screen_width,
                                                     middle_panel_height);
        lv_obj_set_style_pad_top(middle_panel, 41-top_panel_height, 0);
        // 中间框
        lv_obj_t *middle_box = ui_create_column_panel(middle_panel, 318, 158);
        lv_obj_set_align(middle_box, LV_ALIGN_TOP_MID);
        lv_obj_set_style_border_width(middle_box, 1, 0);
        lv_obj_set_style_border_side(middle_box, LV_BORDER_SIDE_FULL, 0);
        lv_obj_set_style_pad_row(middle_box, 14, 0);

        // 文字提示
        lv_obj_t *middle_tip_label = ui_create_default_label(middle_box, "你没有糯米糍道具了哦。\n"
                                                                         "确认花费99金币，\n"
                                                                         "购买1个糯米糍并使用吗？",
                                                             CHINESE_FONT_16_LIGHT);
        lv_obj_set_style_text_line_space(middle_tip_label, 8, 0);
        lv_obj_set_style_text_align(middle_tip_label, LV_TEXT_ALIGN_CENTER, 0);


        // 选择框
        lv_obj_t *middle_btn_box_parent = ui_create_row_default_panel(middle_box);
        lv_obj_set_style_pad_column(middle_btn_box_parent, 26, 0);
        char *btn_str[2] = {"取消", "确认"};
        for (int i = 0; i < sizeof(btn_str)/sizeof(btn_str[0]); ++i) {
            lv_obj_t *middle_btn_box = ui_prompt_dialog(middle_btn_box_parent, btn_str[i], 83, 34, LV_SIZE_CONTENT,
                                                        CHINESE_FONT_16_LIGHT);
            lv_obj_set_style_radius(middle_btn_box, 6, 0);
            lv_obj_set_style_border_width(middle_btn_box, 2, LV_STATE_FOCUSED);
            lv_group_add_obj(screen->group, middle_btn_box);
            lv_obj_add_event_cb(middle_btn_box, ui_elf_buy_prop_tip_screen_event, LV_EVENT_KEY, (void *)i+1);
            if(i == 0){
//                lv_obj_add_state(middle_btn_box, LV_STATE_CHECKED);
                lv_group_focus_obj(middle_btn_box);
            }
            lv_obj_set_style_pad_bottom(middle_btn_box, 4, 0);
        }

    }
}

static void ui_refrsh_buy_prop_tip_screen(elf_game_module_t *module) {
    // 刷新购买信息label
    prop_data_t * prop_info =get_prop_info_by_id(module->data->current_use_prop_id);
    if (prop_info){
        lv_obj_t *main_panel = lv_obj_get_child(module->screen->screen,0);
        lv_obj_t *middle_panel = lv_obj_get_child(main_panel, 1);
        lv_obj_t *middle_tip_label = lv_obj_get_child(lv_obj_get_child(middle_panel,0),0);
        lv_label_set_text_fmt(middle_tip_label, "你没有%s道具了哦。\n"
                                                "确认花费%u金币，\n"
                                                "购买1个%s并使用吗？",prop_info->name,prop_info->coin,prop_info->name);

    }
}


void ui_elf_buy_prop_tip_screen_show(elf_game_module_t *module){
    LOG_INFO_WRAPPER("start");
    if(module){
        if(module->screen->screen == NULL){
            ui_elf_buy_prop_tip_screen_init(module->screen);
        }
    }
    ui_refrsh_buy_prop_tip_screen(module);
    module->screen->current_page = ELF_GAME_BUY_PROP_SCREEN;
    module->screen->deinit_cb = ui_elf_buy_prop_tip_screen_deinit;
}



void ui_elf_buy_prop_tip_screen_deinit(){
    LOG_INFO_WRAPPER("start");
    ui_screen_group_deinit_common(&(elf_game_module.screen->screen), &(elf_game_module.screen->group));
}
